package com.juc.cf;

import java.util.concurrent.*;

/**
 * @auther zzyy
 * @create 2022-01-15 15:20
 */
public class FutureThreadPoolDemo02 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //3个任务，目前开启多个异步任务线程来处理，请问耗时多少？
        System.out.println(Thread.currentThread().getName() + " -----start");

        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        long startTime = System.currentTimeMillis();

        FutureTask<String> futureTask1 = new FutureTask<String>(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return " task1 over";
        });
        threadPool.submit(futureTask1);

        FutureTask<String> futureTask2 = new FutureTask<String>(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return " task2 over";
        });
        threadPool.submit(futureTask2);

        System.out.println(Thread.currentThread().getName() + futureTask1.get());
        System.out.println(Thread.currentThread().getName() + futureTask2.get());

//        try {
//            TimeUnit.MILLISECONDS.sleep(300);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        long endTime = System.currentTimeMillis();
        System.out.println("----costTime: " + (endTime - startTime) + " 毫秒");


        System.out.println(Thread.currentThread().getName() + " -----end");
        threadPool.shutdown();
    }
}
